<template>
  <view class="sku-box">
    <image
      class="close-icon"
      :src="ossImg('others/goods/close.png')"
      @click.stop="emits('close')"
    />
    <view class="goods">
      <image class="cover" :src="product.imageUrl" />
      <view class="goods_info">
        <view class="goods-title">
          {{ product.name }}
        </view>
        <view class="goods-price"> ￥{{ product.lowestPrice || 0 }} </view>
      </view>
    </view>

    <view class="goods-sku">
      <label class="text">规格</label>
      <view
        @click.stop="skuChange(sku)"
        v-for="sku in product.productSkus"
        :key="sku.id"
        class="sku-item"
        :class="{
          current: _value.productSkuId === sku.id,
        }"
      >
        <text>{{ sku.spData[0].value }}</text>
      </view>
    </view>
    <view class="goods-count flex justify-between items-center">
      数量
      <big-stepper v-model:value="_value.quantity" />
    </view>
    <button class="sku-confirm" @click.stop="handleConfirm">确认</button>
  </view>
</template>
  
  <script setup>
import { defineProps, ref, defineEmits, watch } from "vue";
import BigStepper from "@/components/big-stepper";
import { message, ossImg } from "@/utils/utils";

const emits = defineEmits(["confirm", "close", "skuChange"]);
const props = defineProps({
  product: {
    type: Object,
    default: () => ({}),
  },
  productSku: {
    type: Object,
    default: () => ({}),
  },
});

const _value = ref({
  productSkuId: "",
  quantity: 1,
});
watch(
  () => props.productSku,
  () => {
    console.log("监听porps", props.productSku);
    _value.value.productSkuId = props.productSku.giftCardProductSkuId;
    _value.value.quantity = props.productSku.quantity || 1;
  },
  {
    immediate: true, // 这个属性是重点啦
  }
);

const skuChange = (sku) => {
  if (sku.stock === 0) return;
  _value.value.productSkuId = sku.id;
  emits("skuChange", sku.spData[0]);
};

const handleConfirm = () => {
  if (!_value.value.productSkuId && props.product.productSkus.length !== 0)
    return message("请选择商品规格");
  emits("confirm", _value.value);
};
</script>
  
  <style lang="less" scoped>
@import "@/static/css/theme.less";

.sku-box {
  height: 316px;
  box-sizing: border-box;
  padding: 16px 16px 9px;
  position: relative;
  font-family: Source Han Sans CN;
  font-weight: 500;

  .close-icon {
    width: 20px;
    height: 20px;
    position: absolute;
    top: 17px;
    right: 17px;
  }

  .goods-sku {
    padding-top: 2px;
    color: #171717;

    .text {
      height: 23px;
      padding-left: 4px;
      line-height: 23px;
      font-size: 13px;
      display: block;
    }
    .sku-item {
      width: 160px;
      height: 27px;
      line-height: 27px;
      margin-top: 5px;
      text-align: center;
      background: #f6f6f6;
      border-radius: 5px;
      font-size: 12px;
      color: #2b2b2b;
      &.current {
        background-color: #e8effe;
      }

      &.disabled {
        color: #d2d1d1;
        background-color: #f0f1f3;
      }
    }
  }

  .goods-count {
    height: 19px;
    line-height: 19px;
    margin-top: 18px;
    box-sizing: border-box;
    padding-left: 5px;
    padding-right: 2px;
    font-size: 13px;
    color: #171717;
  }

  .sku-confirm {
    width: 343px;
    height: 40px;
    background: #0044c9;
    border-radius: 20px;
    font-size: 16px;
    color: #ffffff;
    margin-top: 15px;
  }
}

.goods {
  display: flex;
  align-items: center;
  height: 80px;

  .cover {
    width: 80px;
    height: 80px;
    border-radius: 5px;
    margin-left: 3px;
    margin-right: 12px;
  }

  .goods_info {
    height: 100%;
    flex: 1;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
  }

  .goods-title {
    overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    width: 222px;
    color: #000;
    font-size: 15px;
  }

  .goods-price {
    height: 18px;
    line-height: 18px;
    font-size: 18px;
    font-weight: 800;
    color: #0044c9;
  }
}
</style>
  